package CustomUI;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.Utility;
import org.sqlStatements;

import DTO.DrawingCmdDTO;
import DTO.DrawingPlaneDTO;
import DTO.Properties;
import DTO.PropertiesDTO;
import GameTools.ydPoint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;

public class LayerView extends View {

	public static DrawingSurface drawingSurface;
	ArrayList<PathPaintSet> tempPaths;
	ArrayList<DrawingCmdDTO> drawingsCmds;

	Bitmap mCurrentDraw;

	Canvas mCanvas;

	Paint paint = new Paint();
	
	Matrix matrix=new Matrix();
	
	Path specialPath=null;
	Paint specialPaint=new Paint();


	public LayerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		matrix.reset();
	}

	@Override
	public void draw(Canvas canvas) {
		//canvas.drawColor(Color.RED);
		if (mCurrentDraw != null) {
			canvas.drawBitmap(mCurrentDraw, matrix, paint);
		}
		
		/*if(specialPath!=null){
			canvas.drawPath(specialPath, specialPaint);
		}*/
	}

	public LayerView(Context context) {
		super(context);
		matrix.reset();
	}

	public void draw(DrawingCmdDTO d) {
		mCurrentDraw = d.image;
		invalidate();

	}

	public void draw(ArrayList<DrawingCmdDTO> DC) {
		int x = getWidth();
		if (x == 0)
			return;
		Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
				Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(bitmap);
		tempPaths = new ArrayList<PathPaintSet>();
		/*
		 * for (DrawingCmdDTO d : DC) { tempPaths.add(new PathPaintSet(d)); }
		 */
		drawingsCmds = (ArrayList<DrawingCmdDTO>) DC.clone();
		// invalidate();

		DrawingCmdDTO d;
		for (int i = 0; i < DC.size(); i++) {
			d = DC.get(i);
			PathPaintSet p = new PathPaintSet(d);
			mCanvas.drawPath(p.path, p.paint);
			// if (!d.command.equals(sqlStatements.DC_CREATE_LAYER)) {
			d.setImage(bitmap);
			// setPaintProperties(d.getProperties());
			// }

			if (d.dcid == -1)
				new MyAsyncTask().execute(d, DC);
			mCurrentDraw = d.image;
		}
		invalidate();
	}

	/*
	 * private void setPaintProperties(PropertiesDTO properties) {
	 * paint.setColor(properties.color);
	 * paint.setStrokeWidth(properties.strokeSize);
	 * 
	 * if (properties.fill == 0) { paint.setStyle(Style.STROKE); } else {
	 * paint.setStyle(Style.FILL); }
	 * 
	 * }
	 */

	public Bitmap getmCurrentDraw() {
		return mCurrentDraw;
	}

	public void setmCurrentDraw(Bitmap mCurrentDraw) {
		this.mCurrentDraw = mCurrentDraw;
	}

	public void transform(int Type, int t, int from, int to) {
		
		int px = getWidth() / 2;
		int py = getHeight() / 2;

		if (t == 1) {
			int delta = 0;
			if (from > to) {
				delta = to-from;
			} else if (from < to) {
				delta = to-from;
			}
			if (Type == 1) {
				matrix.postTranslate(delta, 0);
			} else if (Type == 2) {
				matrix.postTranslate(0, delta);
			}
		} else if (t == 2) {

			float scale = 1;
			if (from > to) {
				scale = 1.05f;
			} else if (from < to) {
				scale = 0.95f;
			}
			if (Type == 1) {
				matrix.postScale(scale, 1, px, py);
			} else if (Type == 2) {
				matrix.postScale(1, scale, px, py);
			}
		} else if (t == 3) {

			int rotate = 1;
			if (from > to) {
				rotate = 1;
			} else if (from < to) {
				rotate = -1;
			}

			matrix.postRotate(rotate, px, py);

		}
		
	/*	mCurrentDraw= Bitmap.createBitmap(mCurrentDraw, 0, 0, 
				mCurrentDraw.getWidth(), mCurrentDraw.getHeight(), matrix, true);*/
		
		invalidate();
		
	}

	public void ConfirmClicked() {
		matrix.reset();
		
	}

	public Bitmap getImage() {
		Bitmap b= Bitmap.createBitmap(getWidth(), getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas c=new Canvas(b);
		draw(c);
		return b;
	}

	public void CancelClicked() {
		matrix.reset();
		
	}

	public void erase(Path currentPath) {
		specialPath=currentPath;
		specialPaint.setStrokeWidth(10);
		specialPaint.setStyle(Style.STROKE);
		specialPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		//Bitmap b=getImage();
		
		Canvas c=new Canvas(mCurrentDraw);
		
		//c.drawBitmap(b, 0, 0, paint);
		Paint p=new Paint();
		p.setStrokeWidth(3);
		p.setStyle(Style.STROKE);
		p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		c.drawPath(currentPath, p);
		invalidate();
	}
}

class MyAsyncTask extends AsyncTask<Object, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Object... objs) {
		// TODO Auto-generated method stub

		if (((DrawingCmdDTO) objs[0]).insertUpdate() > 0) {
			// DrawingCmdDTO.deleteAndAfter(((DrawingCmdDTO) objs[0]).lid,
			// ((DrawingCmdDTO) objs[0]).instructNum);
		}

		return true;
	}

	protected void onPostExecute(Double result) {

	}

	protected void onProgressUpdate(Integer... progress) {

	}
}

class PathPaintSet {
	Path path;
	Paint paint;

	public PathPaintSet(DrawingCmdDTO d) {
		path = new Path(ydPoint.ArrayToPath(d.coordinates));
		paint = new Paint();
		Properties properties = d.getProperties();
		if (properties != null) {
			paint.setColor(properties.color);
			paint.setStrokeWidth(properties.strokeSize);

			if (properties.fill == 0) {
				paint.setStyle(Style.STROKE);
			} else {
				paint.setStyle(Style.FILL);
			}
		}
	}
}
